# https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=40&limit=20    第三页 start 60
# https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=20&limit=20    第二页 start 20
# https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20     第一页 start 0

# start = (page - 1) * 20

# 下载豆瓣电影前十页的数据
url = 'https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=40&limit=20'

# 请求对象定制
# 获取响应数据
# 下载数据
# 40&limit=20'

import urllib.parse
import urllib.request
def create_request(page) :
    base_url = 'https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&'
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76',
    }
    data = {
        'start': str((page-1)*20),
        'limit': '20'
    }
    data = urllib.parse.urlencode(data)
    url = base_url + data
    request = urllib.request.Request(url=url,headers=header)
    return request


def get_content(request) :
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    return content

def download(page , content):
    with open('douban_'+str(page) + '.json' , 'w' , encoding='utf-8') as f:
        f.write(content)

# 程序入口
if __name__ == '__main__':
    start_page = int(input('输入起始页码'))
    end_page = int(input('输入结束页码'))
    for page in range(start_page , end_page + 1):
        request = create_request(page)
        content = get_content(request)
        # 下载
        download(page , content)
